package engine;

import java.util.ArrayList;

/**
 *
 * @author Johnny
 */
public class Unit {

    private int id;
    private String name;
    private ArrayList<Node> course;
    private ArrayList<Instance> instances;

    public Unit(int id, String name, ArrayList<Node> course, ArrayList<Instance> instances) {
        this.id = id;
        this.name = name;
        this.course = course;
        this.instances = instances;
    }

    public Unit(int id, String name) {
        this(id, name, new ArrayList<Node>(), new ArrayList<Instance>());
    }

    public Unit(int id) {
        this(id,"");
    }

    public ArrayList<Node> getCourse() {
        return course;
    }

    public int getId() {
        return id;
    }

    public ArrayList<Instance> getInstances() {
        return instances;
    }

    public String getName() {
        return name;
    }

    /**
     * Returns the number of course nodes.
     * @return the number of course nodes.
     */
    public int getCourseNr() {
        return course.size();
    }

    /**
     * Returns the number of instances.
     * @return the number of instances.
     */
    public int getInstanceNr() {
        return instances.size();
    }

    void setName(String name) {
        this.name = name;
    }

    void setInstances(ArrayList<Instance> instances) {
        this.instances = instances;
    }

    /**
     * Adds a node to the course list.
     * @param node the node to be added.
     */
    void addCourse(Node node) {
        course.add(node);
    }

    /**
     * Removes a node from the courses list.
     * @param nodeId the id of the node to be removed.
     */
    void removeCourse(int nodeId) {
        for (int i = 0; i < course.size(); i++) {
            if (course.get(i).getId() == nodeId) {
                course.remove(i);
                break;
            }
        }
    }

    /**
     * Adds an instance to the instances list.
     * @param inst the instance to be added.
     */
    void addInstace(Instance inst) {
        instances.add(inst);
    }

    /**
     * Removes an instance from the instances list.
     * @param instId the id of the instance to be removed.
     */
    void removeInstance(int instId) {
        for (int i = 0; i < instances.size(); i++) {
            if (instances.get(i).getId() == instId) {
                instances.remove(i);
                break;
            }
        }
    }

    /**
     * Returns the reference of the contained node.
     * @param nodeId the id of the node that will be searched.
     * @return the reference to the node or null if there isn't any.
     */
    Node hasNode(int nodeId) {
        Node dummy = new Node(nodeId);
        int index;
        if ((index = course.indexOf(dummy)) != -1)
            return course.get(index);

        return null;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Unit other = (Unit) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + this.id;
        return hash;
    }

    @Override
    public String toString() {
//        StringBuffer sb = new StringBuffer();
//        sb.append(id);
//        sb.append(' ');
//        sb.append(name);
//        sb.append(" (");
//        for (Node node : course) {
//            sb.append(node.getId());
//            sb.append(" ");
//        }
//        sb.append(")");
//
//        return sb.toString();
        return name;
    }
}
